home *** CD-ROM | disk | FTP | other *** search
/ Kellogg's Amérique / Kellogg's Amérique / main.swf / scripts / fl / controls / ScrollBar.as < prev    next >
Text File  |  2020-08-04  |  14KB  |  430 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.ComponentEvent;
  6.    import fl.events.ScrollEvent;
  7.    import flash.events.MouseEvent;
  8.    
  9.    public class ScrollBar extends UIComponent
  10.    {
  11.       
  12.       private static var defaultStyles:Object = {
  13.          "downArrowDisabledSkin":"ScrollArrowDown_disabledSkin",
  14.          "downArrowDownSkin":"ScrollArrowDown_downSkin",
  15.          "downArrowOverSkin":"ScrollArrowDown_overSkin",
  16.          "downArrowUpSkin":"ScrollArrowDown_upSkin",
  17.          "thumbDisabledSkin":"ScrollThumb_upSkin",
  18.          "thumbDownSkin":"ScrollThumb_downSkin",
  19.          "thumbOverSkin":"ScrollThumb_overSkin",
  20.          "thumbUpSkin":"ScrollThumb_upSkin",
  21.          "trackDisabledSkin":"ScrollTrack_skin",
  22.          "trackDownSkin":"ScrollTrack_skin",
  23.          "trackOverSkin":"ScrollTrack_skin",
  24.          "trackUpSkin":"ScrollTrack_skin",
  25.          "upArrowDisabledSkin":"ScrollArrowUp_disabledSkin",
  26.          "upArrowDownSkin":"ScrollArrowUp_downSkin",
  27.          "upArrowOverSkin":"ScrollArrowUp_overSkin",
  28.          "upArrowUpSkin":"ScrollArrowUp_upSkin",
  29.          "thumbIcon":"ScrollBar_thumbIcon",
  30.          "repeatDelay":500,
  31.          "repeatInterval":35
  32.       };
  33.       
  34.       protected static const THUMB_STYLES:Object = {
  35.          "disabledSkin":"thumbDisabledSkin",
  36.          "downSkin":"thumbDownSkin",
  37.          "overSkin":"thumbOverSkin",
  38.          "upSkin":"thumbUpSkin",
  39.          "icon":"thumbIcon",
  40.          "textPadding":0
  41.       };
  42.       
  43.       public static const WIDTH:Number = 15;
  44.       
  45.       protected static const DOWN_ARROW_STYLES:Object = {
  46.          "disabledSkin":"downArrowDisabledSkin",
  47.          "downSkin":"downArrowDownSkin",
  48.          "overSkin":"downArrowOverSkin",
  49.          "upSkin":"downArrowUpSkin",
  50.          "repeatDelay":"repeatDelay",
  51.          "repeatInterval":"repeatInterval"
  52.       };
  53.       
  54.       protected static const UP_ARROW_STYLES:Object = {
  55.          "disabledSkin":"upArrowDisabledSkin",
  56.          "downSkin":"upArrowDownSkin",
  57.          "overSkin":"upArrowOverSkin",
  58.          "upSkin":"upArrowUpSkin",
  59.          "repeatDelay":"repeatDelay",
  60.          "repeatInterval":"repeatInterval"
  61.       };
  62.       
  63.       protected static const TRACK_STYLES:Object = {
  64.          "disabledSkin":"trackDisabledSkin",
  65.          "downSkin":"trackDownSkin",
  66.          "overSkin":"trackOverSkin",
  67.          "upSkin":"trackUpSkin",
  68.          "repeatDelay":"repeatDelay",
  69.          "repeatInterval":"repeatInterval"
  70.       };
  71.        
  72.       
  73.       private var _direction:String = "vertical";
  74.       
  75.       protected var inDrag:Boolean = false;
  76.       
  77.       protected var upArrow:BaseButton;
  78.       
  79.       private var _pageScrollSize:Number = 0;
  80.       
  81.       protected var downArrow:BaseButton;
  82.       
  83.       private var _pageSize:Number = 10;
  84.       
  85.       private var thumbScrollOffset:Number;
  86.       
  87.       private var _maxScrollPosition:Number = 0;
  88.       
  89.       private var _scrollPosition:Number = 0;
  90.       
  91.       protected var track:BaseButton;
  92.       
  93.       private var _minScrollPosition:Number = 0;
  94.       
  95.       private var _lineScrollSize:Number = 1;
  96.       
  97.       protected var thumb:LabelButton;
  98.       
  99.       public function ScrollBar()
  100.       {
  101.          _pageSize = 10;
  102.          _pageScrollSize = 0;
  103.          _lineScrollSize = 1;
  104.          _minScrollPosition = 0;
  105.          _maxScrollPosition = 0;
  106.          _scrollPosition = 0;
  107.          _direction = ScrollBarDirection.VERTICAL;
  108.          inDrag = false;
  109.          super();
  110.          setStyles();
  111.          focusEnabled = false;
  112.       }
  113.       
  114.       public static function getStyleDefinition() : Object
  115.       {
  116.          return defaultStyles;
  117.       }
  118.       
  119.       public function get minScrollPosition() : Number
  120.       {
  121.          return _minScrollPosition;
  122.       }
  123.       
  124.       public function set minScrollPosition(param1:Number) : void
  125.       {
  126.          setScrollProperties(_pageSize,param1,_maxScrollPosition);
  127.       }
  128.       
  129.       public function setScrollPosition(param1:Number, param2:Boolean = true) : void
  130.       {
  131.          var _loc3_:Number = NaN;
  132.          _loc3_ = scrollPosition;
  133.          _scrollPosition = Math.max(_minScrollPosition,Math.min(_maxScrollPosition,param1));
  134.          if(_loc3_ == _scrollPosition)
  135.          {
  136.             return;
  137.          }
  138.          if(param2)
  139.          {
  140.             dispatchEvent(new ScrollEvent(_direction,scrollPosition - _loc3_,scrollPosition));
  141.          }
  142.          updateThumb();
  143.       }
  144.       
  145.       public function set scrollPosition(param1:Number) : void
  146.       {
  147.          setScrollPosition(param1,true);
  148.       }
  149.       
  150.       public function get pageScrollSize() : Number
  151.       {
  152.          return _pageScrollSize == 0 ? Number(_pageSize) : Number(_pageScrollSize);
  153.       }
  154.       
  155.       public function set pageSize(param1:Number) : void
  156.       {
  157.          if(param1 > 0)
  158.          {
  159.             _pageSize = param1;
  160.          }
  161.       }
  162.       
  163.       public function setScrollProperties(param1:Number, param2:Number, param3:Number, param4:Number = 0) : void
  164.       {
  165.          this.pageSize = param1;
  166.          _minScrollPosition = param2;
  167.          _maxScrollPosition = param3;
  168.          if(param4 >= 0)
  169.          {
  170.             _pageScrollSize = param4;
  171.          }
  172.          enabled = _maxScrollPosition > _minScrollPosition;
  173.          setScrollPosition(_scrollPosition,false);
  174.          updateThumb();
  175.       }
  176.       
  177.       override public function set enabled(param1:Boolean) : void
  178.       {
  179.          super.enabled = param1;
  180.          downArrow.enabled = track.enabled = thumb.enabled = upArrow.enabled = enabled && _maxScrollPosition > _minScrollPosition;
  181.          updateThumb();
  182.       }
  183.       
  184.       protected function updateThumb() : void
  185.       {
  186.          var _loc1_:Number = NaN;
  187.          _loc1_ = _maxScrollPosition - _minScrollPosition + _pageSize;
  188.          if(track.height <= 12 || _maxScrollPosition <= _minScrollPosition || (_loc1_ == 0 || isNaN(_loc1_)))
  189.          {
  190.             thumb.height = 12;
  191.             thumb.visible = false;
  192.          }
  193.          else
  194.          {
  195.             thumb.height = Math.max(13,_pageSize / _loc1_ * track.height);
  196.             thumb.y = track.y + (track.height - thumb.height) * ((_scrollPosition - _minScrollPosition) / (_maxScrollPosition - _minScrollPosition));
  197.             thumb.visible = enabled;
  198.          }
  199.       }
  200.       
  201.       protected function thumbPressHandler(param1:MouseEvent) : void
  202.       {
  203.          inDrag = true;
  204.          thumbScrollOffset = mouseY - thumb.y;
  205.          thumb.mouseStateLocked = true;
  206.          mouseChildren = false;
  207.          stage.addEventListener(MouseEvent.MOUSE_MOVE,handleThumbDrag,false,0,true);
  208.          stage.addEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler,false,0,true);
  209.       }
  210.       
  211.       protected function thumbReleaseHandler(param1:MouseEvent) : void
  212.       {
  213.          inDrag = false;
  214.          mouseChildren = true;
  215.          thumb.mouseStateLocked = false;
  216.          stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleThumbDrag);
  217.          stage.removeEventListener(MouseEvent.MOUSE_UP,thumbReleaseHandler);
  218.       }
  219.       
  220.       public function set pageScrollSize(param1:Number) : void
  221.       {
  222.          if(param1 >= 0)
  223.          {
  224.             _pageScrollSize = param1;
  225.          }
  226.       }
  227.       
  228.       protected function handleThumbDrag(param1:MouseEvent) : void
  229.       {
  230.          var _loc2_:Number = NaN;
  231.          _loc2_ = Math.max(0,Math.min(track.height - thumb.height,mouseY - track.y - thumbScrollOffset));
  232.          setScrollPosition(_loc2_ / (track.height - thumb.height) * (_maxScrollPosition - _minScrollPosition) + _minScrollPosition);
  233.       }
  234.       
  235.       public function set direction(param1:String) : void
  236.       {
  237.          var _loc2_:* = false;
  238.          if(_direction == param1)
  239.          {
  240.             return;
  241.          }
  242.          _direction = param1;
  243.          if(isLivePreview)
  244.          {
  245.             return;
  246.          }
  247.          setScaleY(1);
  248.          _loc2_ = _direction == ScrollBarDirection.HORIZONTAL;
  249.          if(_loc2_ && componentInspectorSetting)
  250.          {
  251.             if(rotation == 90)
  252.             {
  253.                return;
  254.             }
  255.             setScaleX(-1);
  256.             rotation = -90;
  257.          }
  258.          if(!componentInspectorSetting)
  259.          {
  260.             if(_loc2_ && rotation == 0)
  261.             {
  262.                rotation = -90;
  263.                setScaleX(-1);
  264.             }
  265.             else if(!_loc2_ && rotation == -90)
  266.             {
  267.                rotation = 0;
  268.                setScaleX(1);
  269.             }
  270.          }
  271.          invalidate(InvalidationType.SIZE);
  272.       }
  273.       
  274.       public function set lineScrollSize(param1:Number) : void
  275.       {
  276.          if(param1 > 0)
  277.          {
  278.             _lineScrollSize = param1;
  279.          }
  280.       }
  281.       
  282.       override public function get height() : Number
  283.       {
  284.          return _direction == ScrollBarDirection.HORIZONTAL ? Number(super.width) : Number(super.height);
  285.       }
  286.       
  287.       protected function scrollPressHandler(param1:ComponentEvent) : void
  288.       {
  289.          var _loc2_:Number = NaN;
  290.          var _loc3_:Number = NaN;
  291.          param1.stopImmediatePropagation();
  292.          if(param1.currentTarget == upArrow)
  293.          {
  294.             setScrollPosition(_scrollPosition - _lineScrollSize);
  295.          }
  296.          else if(param1.currentTarget == downArrow)
  297.          {
  298.             setScrollPosition(_scrollPosition + _lineScrollSize);
  299.          }
  300.          else
  301.          {
  302.             _loc2_ = track.mouseY / track.height * (_maxScrollPosition - _minScrollPosition) + _minScrollPosition;
  303.             _loc3_ = pageScrollSize == 0 ? Number(pageSize) : Number(pageScrollSize);
  304.             if(_scrollPosition < _loc2_)
  305.             {
  306.                setScrollPosition(Math.min(_loc2_,_scrollPosition + _loc3_));
  307.             }
  308.             else if(_scrollPosition > _loc2_)
  309.             {
  310.                setScrollPosition(Math.max(_loc2_,_scrollPosition - _loc3_));
  311.             }
  312.          }
  313.       }
  314.       
  315.       public function get pageSize() : Number
  316.       {
  317.          return _pageSize;
  318.       }
  319.       
  320.       public function set maxScrollPosition(param1:Number) : void
  321.       {
  322.          setScrollProperties(_pageSize,_minScrollPosition,param1);
  323.       }
  324.       
  325.       public function get scrollPosition() : Number
  326.       {
  327.          return _scrollPosition;
  328.       }
  329.       
  330.       override public function get enabled() : Boolean
  331.       {
  332.          return super.enabled;
  333.       }
  334.       
  335.       override protected function draw() : void
  336.       {
  337.          var _loc1_:Number = NaN;
  338.          if(isInvalid(InvalidationType.SIZE))
  339.          {
  340.             _loc1_ = super.height;
  341.             downArrow.move(0,Math.max(upArrow.height,_loc1_ - downArrow.height));
  342.             track.setSize(WIDTH,Math.max(0,_loc1_ - (downArrow.height + upArrow.height)));
  343.             updateThumb();
  344.          }
  345.          if(isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
  346.          {
  347.             setStyles();
  348.          }
  349.          downArrow.drawNow();
  350.          upArrow.drawNow();
  351.          track.drawNow();
  352.          thumb.drawNow();
  353.          validate();
  354.       }
  355.       
  356.       override protected function configUI() : void
  357.       {
  358.          super.configUI();
  359.          track = new BaseButton();
  360.          track.move(0,14);
  361.          track.useHandCursor = false;
  362.          track.autoRepeat = true;
  363.          track.focusEnabled = false;
  364.          addChild(track);
  365.          thumb = new LabelButton();
  366.          thumb.label = "";
  367.          thumb.setSize(WIDTH,15);
  368.          thumb.move(0,15);
  369.          thumb.focusEnabled = false;
  370.          addChild(thumb);
  371.          downArrow = new BaseButton();
  372.          downArrow.setSize(WIDTH,14);
  373.          downArrow.autoRepeat = true;
  374.          downArrow.focusEnabled = false;
  375.          addChild(downArrow);
  376.          upArrow = new BaseButton();
  377.          upArrow.setSize(WIDTH,14);
  378.          upArrow.move(0,0);
  379.          upArrow.autoRepeat = true;
  380.          upArrow.focusEnabled = false;
  381.          addChild(upArrow);
  382.          upArrow.addEventListener(ComponentEvent.BUTTON_DOWN,scrollPressHandler,false,0,true);
  383.          downArrow.addEventListener(ComponentEvent.BUTTON_DOWN,scrollPressHandler,false,0,true);
  384.          track.addEventListener(ComponentEvent.BUTTON_DOWN,scrollPressHandler,false,0,true);
  385.          thumb.addEventListener(MouseEvent.MOUSE_DOWN,thumbPressHandler,false,0,true);
  386.          enabled = false;
  387.       }
  388.       
  389.       public function get direction() : String
  390.       {
  391.          return _direction;
  392.       }
  393.       
  394.       public function get lineScrollSize() : Number
  395.       {
  396.          return _lineScrollSize;
  397.       }
  398.       
  399.       override public function setSize(param1:Number, param2:Number) : void
  400.       {
  401.          if(_direction == ScrollBarDirection.HORIZONTAL)
  402.          {
  403.             super.setSize(param2,param1);
  404.          }
  405.          else
  406.          {
  407.             super.setSize(param1,param2);
  408.          }
  409.       }
  410.       
  411.       public function get maxScrollPosition() : Number
  412.       {
  413.          return _maxScrollPosition;
  414.       }
  415.       
  416.       override public function get width() : Number
  417.       {
  418.          return _direction == ScrollBarDirection.HORIZONTAL ? Number(super.height) : Number(super.width);
  419.       }
  420.       
  421.       protected function setStyles() : void
  422.       {
  423.          copyStylesToChild(downArrow,DOWN_ARROW_STYLES);
  424.          copyStylesToChild(thumb,THUMB_STYLES);
  425.          copyStylesToChild(track,TRACK_STYLES);
  426.          copyStylesToChild(upArrow,UP_ARROW_STYLES);
  427.       }
  428.    }
  429. }
  430.